package com.ajview.common.core.domain.vo;

import com.ajview.common.core.domain.enums.ResultCodeEnum;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;

/**
 * @author ZhangJunJie
 * @Date 2022-04-26
 * <p> 视图模型
 * 概要：通用返回值
 */
public class ResultVo implements Serializable {

    private static final long serialVersionUID = 1226543114677512135L;
    private static final String CODE = "code";
    private static final String MESSAGE = "msg";
    private static final String DATA = "data";

    public static JSONObject result(ResultCodeEnum codeEnum) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(CODE, codeEnum.getCode());
        jsonObject.put(MESSAGE, codeEnum.getMessage());
        jsonObject.put(DATA, new JSONObject());
        return jsonObject;
    }

    public static JSONObject result(ResultCodeEnum codeEnum, Object resultData) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(CODE, codeEnum.getCode());
        jsonObject.put(MESSAGE, codeEnum.getMessage());
        jsonObject.put(DATA, resultData);
        return jsonObject;
    }

    public static JSONObject result(Integer code, String msg, Object resultData) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(CODE, code);
        jsonObject.put(MESSAGE, msg);
        jsonObject.put(DATA, resultData);
        return jsonObject;
    }

    public static JSONObject success() {
        return result(ResultCodeEnum.REQUEST_SUCCESS);
    }

    public static JSONObject success(Object resultData) {
        return result(ResultCodeEnum.REQUEST_SUCCESS, resultData);
    }

    public static JSONObject success(String msg, Object resultData) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(CODE, ResultCodeEnum.REQUEST_SUCCESS.getCode());
        jsonObject.put(MESSAGE, msg);
        jsonObject.put(DATA, resultData);
        return jsonObject;
    }


    public static JSONObject error() {
        return result(ResultCodeEnum.REQUEST_ERROR);
    }

    public static JSONObject error(String msg) {
        return error(msg, new JSONObject());
    }

    public static JSONObject error(Object resultData) {
        return result(ResultCodeEnum.REQUEST_ERROR, resultData);
    }

    public static JSONObject error(String msg, Object resultData) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put(CODE, ResultCodeEnum.REQUEST_ERROR.getCode());
        jsonObject.put(MESSAGE, msg);
        jsonObject.put(DATA, resultData);
        return jsonObject;
    }


}
